package com.esun.service.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.util.CollectionUtils;
import com.esun.common.constant.SystemsConstant;
import com.esun.common.enums.Message;
import com.esun.common.utils.FileUtils;
import com.esun.common.utils.GuidUtils;
import com.esun.common.utils.MessageUtils;
import com.esun.dao.das.OperationLogDas;

import com.esun.api.request.OperationLogRequestDto;
import com.esun.api.response.OperationLogResponseDto;
import com.esun.dao.eo.*;
import com.esun.service.service.OperationLogService;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (OperationLog)表服务实现类
 *
 * @author baron.wubo
 * @since 2021-10-13 13:28:35
 */
@Service
public class OperationLogServiceImpl implements OperationLogService {
    @Resource
    private OperationLogDas operationLogDas;
    @Value("${file.path:/}")
    String filePath;
    @Override
    public PageInfo<OperationLogResponseDto> getOperationLogByPage(OperationLogRequestDto requestDto) {
        List<OperationLogResponseDto> responseDtoList = new ArrayList<>();
        PageEo pageEo = PageEo.builder().pageIndex(requestDto.getPageIndex()).pageSize(requestDto.getPageSize()).build();
        OperationLogEo operationLogEoEo = OperationLogEo.builder().build();
        BeanUtils.copyProperties(requestDto,operationLogEoEo);
        List<SortEo> sortList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(requestDto.getSortList())){
            requestDto.getSortList().forEach((sortRequestDto -> {
                sortList.add(SortEo.builder().sortParam(sortRequestDto.getSortParam()).ascending(sortRequestDto.getAscending()).build());
            }));
        }
        PageInfo<OperationLogEo> pageInfo = operationLogDas.queryPageBySort(operationLogEoEo,pageEo, sortList);
        List<OperationLogEo> eoList = pageInfo.getList();
        for (OperationLogEo eo : eoList){
            OperationLogResponseDto responseDto = new OperationLogResponseDto();
            BeanUtils.copyProperties(eo,responseDto);
            responseDtoList.add(responseDto);
        }
        PageInfo<OperationLogResponseDto> responseDtoPageInfo = new PageInfo<>(responseDtoList);
        responseDtoPageInfo.setTotal(pageInfo.getTotal());
        return responseDtoPageInfo;
    }

    @Override
    public OperationLogResponseDto getLogErrorTotal() {
        OperationLogEo operationLogEo = new OperationLogEo();
        operationLogEo.setStatus(1);
        int total = operationLogDas.errorTotal(operationLogEo);
        OperationLogResponseDto operationLogResponseDto = new OperationLogResponseDto();
        operationLogResponseDto.setTotal(total);
        return operationLogResponseDto;
    }


    @Override
    public List<OperationLogResponseDto> addOperationLog(List<OperationLogRequestDto> requestDtoList) {
        List<OperationLogEo> eoList = new ArrayList<>();
        List<OperationLogResponseDto> responseDtoList = new ArrayList<>();
        for (OperationLogRequestDto requestDto : requestDtoList){
            OperationLogEo operationLogEoEo = OperationLogEo.builder().build();
            BeanUtils.copyProperties(requestDto,operationLogEoEo);
            OperationLogResponseDto responseDto = new OperationLogResponseDto();
            BeanUtils.copyProperties(operationLogEoEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(operationLogEoEo.getUserName()+ MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(operationLogEoEo);
        }
        //syncExampleToMongo(eoList);
        for (OperationLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        if (!CollectionUtils.isEmpty(eoList)){
            operationLogDas.insertBatch(eoList);
        }
        return responseDtoList;
    }

    @Override
    public List<OperationLogResponseDto> updateOperationLog(List<OperationLogRequestDto> requestDtoList) {
        List<OperationLogEo> eoList = new ArrayList<>();
        List<OperationLogResponseDto> responseDtoList = new ArrayList<>();
        for (OperationLogRequestDto requestDto : requestDtoList){
            OperationLogEo operationLogEoEo = OperationLogEo.builder().build();
            BeanUtils.copyProperties(requestDto,operationLogEoEo);
            OperationLogResponseDto responseDto = new OperationLogResponseDto();
            BeanUtils.copyProperties(operationLogEoEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(operationLogEoEo.getUserName()+ MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(operationLogEoEo);
        }
        for (OperationLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        for (OperationLogEo eo: eoList) {
            operationLogDas.update(eo);
        }
        return responseDtoList;
    }

    @Override
    public List<OperationLogResponseDto> deleteOperationLog(List<OperationLogRequestDto> requestDtoList) {
        List<OperationLogEo> eoList = new ArrayList<>();
        List<OperationLogResponseDto> responseDtoList = new ArrayList<>();
        for (OperationLogRequestDto requestDto : requestDtoList){
            OperationLogEo operationLogEoEo = OperationLogEo.builder().build();
            BeanUtils.copyProperties(requestDto,operationLogEoEo);
            OperationLogResponseDto responseDto = new OperationLogResponseDto();
            BeanUtils.copyProperties(operationLogEoEo,responseDto);
            String code = Message.QUERY_SUCCESS.getCode();
            responseDto.setErrorCode(code);
            responseDto.setMsg(operationLogEoEo.getUserName()+ MessageUtils.getInfoByCode(code));
            responseDtoList.add(responseDto);
            eoList.add(operationLogEoEo);
        }

        for (OperationLogResponseDto responseDto : responseDtoList) {
            if (!Message.QUERY_SUCCESS.getCode().equals(responseDto.getErrorCode())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return responseDtoList;
            }
        }
        if(!CollectionUtils.isEmpty(eoList)){
            operationLogDas.deleteByIdBatch(eoList);
        }
        return responseDtoList;
    }

    @Override
    public List<OperationLogResponseDto> importOperationLog(Workbook workbook) {
        return null;
    }

    @Override
    public void exportOperationLog(OperationLogRequestDto requestDto) {
        OperationLogEo operationLogEo = OperationLogEo.builder().build();
        BeanUtils.copyProperties(requestDto,operationLogEo);
        List<OperationLogEo> eoList = operationLogDas.queryAll(operationLogEo);
        String programUrl = System.getProperty("user.dir");
        String fileName = programUrl.concat(filePath).concat(GuidUtils.getGuid()).concat(SystemsConstant.HEADQUARTERS);
        EasyExcel.write(fileName, ExampleEo.class).sheet("用户信息导出文件").doWrite(eoList);
        FileUtils fileUtils = new FileUtils();
        fileUtils.downLoad(fileName);
    }
    @Override
    public void downloadTemplate(String path) {

    }
}
